package com.fanbai.score.common.Manager;

import android.app.Activity;
import android.util.Log;


import com.fanbai.score.common.Utils.log.YD_Log;

import java.util.NoSuchElementException;
import java.util.Stack;

/**
 * 主要功能：退出应用时，将所有的Activity都清理掉
 *
 * @author Administrator
 */
public class ActivityManager {


    private static Stack<Activity> activityStack;
    private static ActivityManager instance;

    private ActivityManager() {
    }

    /**
     * 单一实例
     */
    public static ActivityManager getInstance() {
        if (instance == null) {
            instance = new ActivityManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        try {
            Activity activity = activityStack.lastElement();
            finishActivity(activity);
        } catch (NoSuchElementException e) {
            YD_Log.writeLog("error", "finishActivity:NoSuchElementException: " + e.getMessage());
        }
    }

    /**
     * 结束指定的Activity
     */
    private void finishActivity(Activity activity) {
        activityStack.remove(activity);
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
        }else{
            finishActivity();
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        int size = activityStack.size();
        Log.i("ddddd", "total is " + size);

        for (int i = size - 1; i >= 0; i--) {
            try {
                Activity aty = activityStack.get(i);
                if (null != aty) {
                    finishActivity(aty);
                    Log.i("ddddd", i + aty.getLocalClassName() + "is killed");
                } else {
                    Log.i("ddddd", i + "is null");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        activityStack.clear();
    }

    /**
     * 获取指定的Activity
     *
     * @author kymjs
     */
    public Activity getActivity(Class<?> cls) {
        if (activityStack != null)
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) {
                    return activity;
                }
            }
        return null;
    }

    /**
     * 退出应用程序
     */
    public void exitApp() {
        try {
            finishAllActivity();
            // 杀死该应用进程
            android.os.Process.killProcess(android.os.Process.myPid());
            // System.exit(0);
        } catch (Exception e) {
            // String ss = e.getMessage();
            System.out.println("exit APP Exception");
        }
    }
}
